/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.webos.java.base.uprofile;

import org.json.JSONObject;
import org.urchr.web.db.DBSession;
import org.webos.java.base.Dialog;
import org.webos.java.base.DialogType;
import org.webos.java.base.NetworkManager;
import org.webos.java.base.RemoteMessage;
import org.webos.java.base.SystemManager;
import org.webos.java.base.db.users.DBUser;
import org.webos.java.base.UserManager;
import org.webos.java.base.utils.MD5;
import org.webos.java.root.Log;

/**
 *
 * @author Kostya
 */

@DialogType(jsClass="uprofile.Dialog")
public class UserProfileEditor extends Dialog<UserProfileApp>{

    public UserProfileEditor(UserProfileApp aUserProfile) {
        super(aUserProfile);
        getData().putOpt("time_zone", getApplication().getSession().getTimeZone().getID());
        show();
    }

    @Override
    @RemoteMessage
    public void closeDialog(JSONObject data) {
        super.closeDialog(data);
        getApplication().stopApplication(null);
    }


    @RemoteMessage
    public JSONObject okClick(JSONObject aData){
        DBSession dbs = new DBSession();
        try {
            DBUser user = (DBUser) dbs.createQuery("from DBUser where code=:code").
                    setString("code", getApplication().getSession().getUserCode()).uniqueResult();
            if(user==null)
                return NetworkManager.getErrorObject(1101, "Can't find user in DB");
            dbs.begin();
            user.setTimeZone(aData.optString("time_zone", SystemManager.getProperty("default_time_zone", "Asia/Tokyo")));
            String oldPassword = aData.optString("old_pass", "");
            String newPassword = aData.optString("new_pass", "");
            String newPassword2 = aData.optString("new_pass2", "");
            boolean announcePassword = false;
            if(!oldPassword.equals("")){
                if(!oldPassword.equals(getApplication().getSession().getPassword())){
                    dbs.rollback();
                    return NetworkManager.getErrorObject(1102, "Invalid old password provided");
                }
                if("".equals(newPassword)){
                    dbs.rollback();
                    return NetworkManager.getErrorObject(1103, "New password can't be empty");
                }
                if(!newPassword2.equals(newPassword)){
                    dbs.rollback();
                    return NetworkManager.getErrorObject(1104, "New passwords don't match");
                }
                user.setPassword(MD5.makeHash(newPassword));
                announcePassword = true;
            }
            dbs.commit();
            if(announcePassword){
                UserManager.passwordChanged(getApplication().getSession().getUserCode(), oldPassword, newPassword);
            }
            closeDialog(null);
        } catch (Throwable t) {
            dbs.rollback();
            Log.error(this, "Exception thrown: " + t.getMessage());
            t.printStackTrace();
        } finally {
            dbs.close();
        }
        return null;
    }

}
